<template>
	<view class="box">
		<!-- logo -->
		<view class="login_title">
			欢迎登录小U商城
		</view>
		<image class="login_logo"  src="../../static/logo/orange.png" alt="" />
		<button class="login_but" hover-class="touchOpacity" open-type="getPhoneNumber" @getphonenumber="_wxLogin">
			微信用户一键登录
		</button>
		<view  class="login_text" hover-class="touchOpacity" @click="goPhoneLogin">手机号验证注册/登录</view>
	</view>
</template>

<script setup>
import http from '../../common/http.js'
const _wxLogin = (e)=>{
	// console.log(e);
	// #ifndef MP-WEIXIN
		uni.showToast({
			title:'此平台不支持微信一键登录',
			icon:'error'
		})
	// #endif
	// #ifdef MP-WEIXIN
		// console.log(e.detail);
		if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){  // 用户拒绝了请求
			uni.showToast({
				title:'您拒绝了授权',
				icon:'error'
			})
			return
		}
		// 用户同意
		let { iv, encryptedData} = e.detail
		// console.log(iv,encryptedData);
		wx.login({
			success: async (res)=>{
				let code = res.code
				let a = {
					url:'/wxlogin',
					data:{iv,encryptedData,code},
					method:'POST'
				}
				let result = await http(a)
				if(result.data.code == 200){
					// 返回信息存储到缓存中
					uni.setStorageSync('userinfo', result.data.list)
					uni.showToast({
						title:result.data.msg
					})
					setTimeout(()=>{
						uni.switchTab({
							url:'/pages/user/user'
						})
					},2000)
				}else{
					uni.showToast({
						title: result.data.msg,
						icon:'error'
					})
				}
			}
		})
	// #endif
}
</script>

<style>
	/* 导入外部的样式文件 */
	@import url("@/static/css/login.css");
</style>
